from random import *


def randomperm(N):
    R = [i for i in range(0, N)]
    shuffle(R)
    return R


def Mutate(Selch, Pm):
    """
    变异操作
    :param Selch:被选择的个体
    :param Pm: 变异概率
    :return: 变异后的个体
    """
    (NSel, L) = Selch.shape
    for i in range(NSel):
        if Pm >= random():
            R = randomperm(L)
            index = R[0:2]
            Selch[i][index[0]], Selch[i][index[1]] = Selch[i][index[1]].copy(), Selch[i][index[0]].copy()
    return Selch
